package spring.chatroom.java_chatroom.common.utils;

import jakarta.servlet.http.HttpSession;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 将userId和session映射起来表示这个人是登录的了
 */
public class SessionTracker {

    private static ConcurrentHashMap<Integer, HttpSession> sessionMap = new ConcurrentHashMap<>();

    public static void setAttribute(Integer userId, HttpSession session) {
        sessionMap.put(userId, session);
    }


    public static HttpSession getAttribute(Integer userId) {
        return sessionMap.getOrDefault(userId, null);
    }


    public static HttpSession removeAttribute(Integer userId) {
        return sessionMap.remove(userId);
    }

    public static Map<Integer, HttpSession> getSessionMap() {
        return sessionMap;
    }

}
